//
//  NearbyViewController.swift
//  Jock
//
//  Created by HD on 15/4/3.
//  Copyright (c) 2015年 Haidy. All rights reserved.
//

import UIKit

//附近的人
class NearbyViewController: UIViewController, BMKLocationServiceDelegate, BMKGeoCodeSearchDelegate {
    
    private var scrollView: UIScrollView!
    private var locationService: BMKLocationService!
    private var search: BMKGeoCodeSearch!

    private var location: CLLocation!

    private var page = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        search = BMKGeoCodeSearch()
        search.delegate = self
        
        locationService = BMKLocationService()
        locationService.delegate = self
        locationService.startUserLocationService()
        
        self.title = "附近"
        var view = ViewUtil.instanceView("NearbyView") as! NearbyView
        scrollView = view.scrollView
        
        self.view.addSubview(view)
        initData()
    }
    
    private func initData() {
    }
    
    private func loadData(locationInfo: String) {
        HttpClient().Post("http://nearby.qiushibaike.com/nearby/fetch", para: createHttpBody(locationInfo), response: { (response) -> Void in
            println(response.text())
        }) { (error) -> Void in
            
        }
    }
    
    //gb2312编码
    class var GB2312: NSStringEncoding {
        return CFStringConvertEncodingToNSStringEncoding(0x0632)
    }
    
    private func createHttpBody(locationInfo: String) -> NSData {
        var json: JSON = ["page": page, "gender": "F", "haunt":locationInfo, "longitude":location.coordinate.longitude, "time":1234, "latitude":location.coordinate.latitude, "count": 30, "stroll": 0]
        return json.description.toData()
    }
    
    func didUpdateBMKUserLocation(userLocation: BMKUserLocation!) {
        if userLocation.location != nil {
            location = userLocation.location
            self.locationService.stopUserLocationService()
            var option = BMKReverseGeoCodeOption()
            option.reverseGeoPoint = location.coordinate
            search.reverseGeoCode(option)
        }
//        println("didUpdateUserLocation:(latitude:\(userLocation.location.coordinate.latitude),latitude:\(userLocation.location.coordinate.longitude))")
    }
    
    func onGetReverseGeoCodeResult(searcher: BMKGeoCodeSearch!, result: BMKReverseGeoCodeResult!, errorCode error: BMKSearchErrorCode) {
        if error.value == BMK_SEARCH_NO_ERROR.value {
            println(result.addressDetail.city)
            println(result.addressDetail.district)
            let city = result.addressDetail.city
            let district = result.addressDetail.district
            let cityPinyin = city.subStringToIndex(city.length - 1).pinyin().replace(" ", to: "")
            let districtPinyin = district.subStringToIndex(city.length - 1).pinyin().replace(" ", to: "")
            println(cityPinyin.startWithUppercaseString())
            println(districtPinyin.startWithUppercaseString())
            
            loadData("\(cityPinyin.startWithUppercaseString()) 路 \(districtPinyin.startWithUppercaseString())")
        } else {
            println("没有获取信息")
        }
    }

}
